
import com.dynamicflash.utils.Delegate;
import com.proalias.animation.EasingLib;
import com.proalias.animation.tween.Wait;
 
	/**
	 * @author Alias Cummins
	 * 
	 */
class com.proalias.animation.TweenManager {

	
	private var $queueArray:Array;
	private var $totalQueues:Number;
	private var $working:Boolean;
	private var $clip:MovieClip;
	private var $intervalID:Number;
	private var $managerClip:MovieClip;
	private static var $instance:TweenManager;
	
	public var easingLib:EasingLib;
	
	// these three methods sigs are required by EventDispatcher
	public var addEventListener:Function;
	public var removeEventListener:Function;
	public var dispatchEvent:Function;
	
	
	public function TweenManager(){
		init();
	}
	
	public function init() : Void {
		//trace("TransitionManager.TransitionManager()");
		$queueArray = new Array();
		$working = false;
		$totalQueues = 0;
		easingLib = new EasingLib();
		$managerClip = _root.createEmptyMovieClip("TxManager",-9999);
	}
	
	public function stopQueue() : Void {
		//trace("TweenManager.stopQueue()");
		$managerClip.onEnterFrame = null;
		$working = false;
	}
	public function startQueue() : Void {
		//trace("TweenManager.startQueue()");
		$managerClip.onEnterFrame = executeQueue;
		$working = true;
	}
	
	public function clearAllTweens() : Void {
		//trace("TweenManager.clearAllTweens()");
		for (var i in $queueArray){
			delete $queueArray[i];
		}
		$queueArray = new Array();
	}
	
	public function executeQueue() : Void {
		//trace("TransitionManager.executeQueue()" + this);
		//trace("TweenManager.executeQueue::$queueArray.length="+$queueArray.length);;
		var i:String;
		var tr:String;
		for (i:String in $queueArray){
			tr+=":"+i;
			if (typeof $queueArray[i] == "object"){
				var tweenStatus:Boolean;
				$working = true;
				tweenStatus = $queueArray[i][0].execute();
				if (!tweenStatus){
					//trace("TweenManager.executeQueue::"+i+" next object");
					var nextItem:Boolean;
					nextItem = $queueArray[i].shift();
					if (!nextItem){
						//trace("TweenManager.executeQueue::queue "+i+" finished");
						delete $queueArray[i];
						$totalQueues --;
					}
				}
			}
		}
		//trace("TweenManager.executeQueue::queues:"+tr);
		//trace("TweenManager.executeQueue::$queueArray.length="+$queueArray.length);
		if ($totalQueues == 0){
			//trace("TweenManager.executeQueue::all queues empty");
			$working = false;
			doneAllTransitions();
		}
	}
	
	private function doneAllTransitions() : Void {
		//trace("TransitionManager.doneAllTransitions()");
		$working = false;
		$managerClip.onEnterFrame = null;
		dispatchEvent({type:"allTransitionsDone", data:"blahblah"});
	}
	
	public function transitionCompleted(transitionEvent:Object):Void {
		//trace("TransitionManager.transitionCompleted("+transitionEvent+")");
		transitionEvent.type = "transitionDone";
		dispatchEvent(transitionEvent);
	}
	
	public function get working():Boolean {
		//trace("TransitionManager.working()");
		return $working;
	}
	
	private function addQueue(name:String):Void{
		//trace("TransitionManager.addQueue("+name+")");
		$queueArray[name] = new Array();
		$totalQueues ++;
	}
	public function removeQueue(name:String):Void{
		for (var i in $queueArray){
			//trace("$queueArray[i]: " + $queueArray[i]+", name: " + name);
			
			//fixed rather nasty comparison bug here, should actually work now! - ar - 16/7
			if(i == name){
				delete $queueArray[i];
				if($totalQueues>0){
					$totalQueues --;
				}
			}
		}
		
	}
	public function addTween(txObject:Object,queue:String):Boolean{
		//trace("TweenManager.addTween("+txObject+","+queue+")");
		if (queue == undefined){
			queue = "default";
		}
		if ($queueArray[queue] == undefined){
			addQueue(queue);
			//$working = true;
			//$managerClip.onEnterFrame = Delegate.create(this,executeQueue);
		}
		if ($queueArray[queue] != undefined){
			$queueArray[queue].push(txObject);
			$working = true;
			$managerClip.onEnterFrame = Delegate.create(this,executeQueue);
			return true;
		}
	}
	
	public function wait(queue:String,frames:Number) : Void {
		//trace("TweenManager.wait("+queue+","+ frames+")");
		var w:Wait = new Wait(frames);
		addTween(w,queue);
	}
	
	/**
	 * @return singleton instance of TransitionManager
	 */
	public static function get instance() : TweenManager {
		//trace("TransitionManager.instance()");
		if ($instance == null)
			$instance = new TweenManager();
		return $instance;
	}
}